/*
-------------------------------------------------------------------
BIE is Copyright 2001-2004 Brunswick Corp.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/bie_license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------

*/
/*
 * LicenseGenerator.java
 *
 * Created on January 7, 2003, 4:34 PM
 */

package tests.jd;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author  jd
 * @version
 */
public class LicenseGenerator extends HttpServlet {
    
    /** Initializes the servlet.
     */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        
    }
    
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
        Connection conn = null;
        HttpSession session = request.getSession();
        String numroutes = request.getParameter("numroutes");
        
        String hostname = request.getParameter("hostname");
        String versionnumber = request.getParameter("versionnumber");
        String expday = request.getParameter("expday");
        String expmonth =request.getParameter("expmonth");
        String expyear = request.getParameter("expyear");
        
        String exp = expmonth + "/" + expday + "/" + expyear;
        
        //RequestlicenseForm reqForm;
        /*
        License lic = new License();
        lic.setHostname(hostname);
        lic.setHostname(hostname);
        lic.setNumroutes(numroutes);
        lic.setVersionnumber(Integer.parseInt(versionnumber));
        */
        
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
        Date d = sdf.parse(exp, new ParsePosition(0));
        long expirydate = d.getTime();
        
        
        // LicenseDAO ldao = new LicenseDAO();
        String hostKey = null;
        
        /*
        EncryptDecrypt ed = new EncryptDecrypt();
        ed.init();
        lic.setPrivatekey1(ed.getN().toString(16));
        lic.setPrivatekey2(ed.getD().toString(16));
        lic.setPublickey1(ed.getN().toString(16));
        lic.setPublickey2(ed.getE().toString(16));
        lic.setExpirydate(expirydate);
        lic.setHostname(hostname);
        lic.setNumroutes(numroutes);
        lic.setVersionnumber(Integer.parseInt(versionnumber));
        */
        
        Prop
        
        
        String licKey = hostname + "|" + numroutes + "|" + versionnumber +
        "|" + expirydate;
        String ciphertext = ed.encrypt(licKey).toUpperCase();
        lic.setLicensekey(ed.getN() + "|" + ed.getD() + "|" + ciphertext);
                                                        
        StringBuffer sql = new StringBuffer();
        sql.append(" insert into LicenseKey (hostName, privateKey1, privateKey2,");
        sql.append(" publicKey1, publicKey2, licenseKey, numRoutes, expiryDate, versionNumber) ");
        sql.append(" values (?,?,?,?,?,?,?,?,?)");
        
        try {
            javax.sql.DataSource dataSource =  servlet.findDataSource(null);
            conn = dataSource.getConnection();
            conn.setAutoCommit(false);
            PreparedStatement ps = conn.prepareStatement(sql.toString());
            ps.setString(1, hostname);
            ps.setString(2, ed.getN().toString(16));
            ps.setString(3, ed.getD().toString(16));
            ps.setString(4, ed.getN().toString(16));
            ps.setString(5, ed.getE().toString(16));
            ps.setString(6, ed.getN() + "|" + ed.getD() + "|" + ciphertext);
            ps.setString(7, numroutes);
            ps.setLong(8, expirydate);
            ps.setInt(9, Integer.parseInt(versionnumber));
            ps.execute();
            conn.commit();
        }
        catch (SQLException sqle) {
            
        }
        finally {
            try {
                conn.close();
            }
            catch (SQLException e) {
                
            }
        }
        
        // ldao.insertLicense(lic);
        // saveToken(request);
        request.setAttribute("License", lic);
        // reqForm.setAction("Create");
        
        processRequest(request, response);
    }
    
}
